﻿namespace Blaze.ConfigExportTool
{
    using System;
    using System.IO;
    using Aspose.Cells;
    using Serialization;

    /// <summary>
    /// 导出器的基类。
    /// </summary>
    public class ExporterBase
    {
        /// <summary>
        /// 获取或设置一个值，表示是否导出开发环境专用的配置。
        /// </summary>
        public bool IsDevelopment { get; set; }

        public static void ExportToClient<TContent>(TContent content, string fileName)
        {
            var path = Path.Combine(mClientDataPath, fileName);
            export<TContent, LuaSerializer>(content, path);
        }

        public static void ExportToClient<TContent, TSerializer>(TContent content, string fileName) where TSerializer : SerializerBase
        {
            var path = Path.Combine(mClientDataPath, fileName);
            export<TContent, TSerializer>(content, path);
        }

        public static void ExportToServer<TContent>(TContent content, string fileName)
        {
            var path = Path.Combine(mServerDataPath, fileName);
            export<TContent, PythonSerializer>(content, path);
        }

        public static void ExportToServer<TContent, TSerializer>(TContent content, string fileName) where TSerializer : SerializerBase
        {
            var path = Path.Combine(mServerDataPath, fileName);
            export<TContent, TSerializer>(content, path);
        }

        public void Execute()
        {
            Initialize();
            ExportForClient();
            ExportForServer();
        }

        /// <summary>
        /// 导出客户端配置。
        /// </summary>
        protected virtual void ExportForClient()
        {
        }

        /// <summary>
        /// 导出服务器配置。
        /// </summary>
        protected virtual void ExportForServer()
        {
        }

        protected Workbook GetWorkbook(string name)
        {
            var path = Path.Combine(Environment.CurrentDirectory, "../../Configs/" + name);
            var workbook = new Workbook(path);
            return workbook;
        }

        /// <summary>
        /// 初始化。
        /// </summary>
        protected virtual void Initialize()
        {
        }

        private static void ensureDirectoryExist(string fullPath)
        {
            var dir = Path.GetDirectoryName(fullPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
        }

        private static void export<TContent, TSerializer>(TContent content, string path) where TSerializer : SerializerBase
        {
            var serializerType = typeof (TSerializer);
            var contentType = typeof (TContent);
            var serializer = (SerializerBase) Activator.CreateInstance(serializerType, contentType);

            if (serializer is PythonSerializer)
                path += ".py";
            else if (serializer is LuaSerializer)
                path += ".lua";
            else if (serializer is XmlSerializer)
                path += ".xml";
            else if (serializer is JsonSerializer)
                path += ".json";
            else
                throw new NotSupportedException();

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, content);
                stream.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(stream);
                var text = reader.ReadToEnd();
                ensureDirectoryExist(path);
                File.WriteAllText(path, text);
            }
        }

        private static readonly string mClientDataPath = Path.Combine(Environment.CurrentDirectory, "../../Configs/ClientData/");
        private static readonly string mServerDataPath = Path.Combine(Environment.CurrentDirectory, "../../Configs/ServerData/");
    }
}